<?php

namespace app\common\service;

use app\admin\model\AdminPhoto;
use think\exception\ValidateException;
use think\Validate;

class UploadService
{

    /**
     *通用上传
     */
    static function commonFile($file, $path = 'common')
    {

        if (get_config('file', 'file-type') == 2) {
            //阿里云上传
            $res = OssService::alYunOSS($file, $file->getUploadExtension(), $path);
            if ($res["code"] == 201) {
                return ['msg' => '上传失败', 'code' => 201, 'data' => $res["msg"]];
            }
            $name = $res['src'];
            $type = 2;
        } else {
            $name = 'uploads'.DS.date("Ymd").DS.time().rand(10000, 99999).'.'.$file->getUploadExtension();    // 文件名称
            $info = $file->move(public_path().DS.$name);
            $name = str_replace("\\", "/", $name);
            $type = 1;
        }
        (new AdminPhoto)->add($info, $name, $type, $file->getUploadMineType());
        return ['msg' => '上传成功', 'code' => 0, 'data' => ['src' => $name, 'thumb' => $name]];
    }
}